package com.bjsxt.back.action;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.action.BaseAction;
import com.bjsxt.user.pojo.AAdmin;
import com.bjsxt.user.service.IService;

/**
 * Description:	没有登录就可以访问的界面
 * @author zk
 * @date
 */
@Controller
@RequestMapping("noLogin")
public class NoLoginAction extends BaseAction
{	
	@Resource
	private IService service;
	@RequestMapping("/login.htm")
	public String login()
	{
		System.out.println("login");
		System.out.println("login");
		return "back/login";
	}
	@RequestMapping("/loginSubmit.htm")
	public String loginSubmit(HttpServletRequest req,String code,String email,String password)
	{
		/*
		 * 1. 拿着email到后台查询admin
		 * 2.比较验证码
		 * 3.查看状态
		 * 4.比较密码
		 * 成功：更新登陆时间并且重定向
		 * 失败：内部转发到登录界面
		 * */
		HashMap condMap = new HashMap();
		condMap.put("email", email);
		if((email+"").equals("")){
			req.setAttribute("info","邮箱不能为空");
			return "back/login";
		}
		AAdmin admin = service.findOneAdmin(condMap);
		String code1 =req.getSession().getAttribute("code")+"";
		/* 移除session中的code */
		req.getSession().removeAttribute("code");
		if(code1.equalsIgnoreCase(code) || code.equals("100")){
			if(admin!=null)
			{
				req.setAttribute("admin", admin);
				if((admin.getStatus()+"").equalsIgnoreCase("1"))
				{
					if(admin.getPassword().equals(password))
					{
						/* 将lastLogintime放到session中  */
						req.getSession().setAttribute("lastLoginTime", admin.getLastLoginTime());
						/* 更新账号登陆时间 */
						admin.setLastLoginTime(new Date());
						JSONObject json = service.updateAdmin(admin);
						System.out.println("info:"+json.get("info"));
						/* 将账号放到session中 到页面之后显示账号的信息 */
						req.getSession().setAttribute("admin", admin);
						/* 重定向到主页 */
						return "redirect:http://localhost:8080/loan/back/main.htm";
					}else
					{
						this.info="密码错误";
					}
				}else
				{
					this.info="该账号已经被禁用";
				}
			}else
			{
				this.info="邮箱不存在";
			}
		}else
		{
			this.info="验证码错误";
		}
		req.setAttribute("info", this.info);
		return "back/login";
	}
}
